﻿
using System.Collections.Generic;
using ModularAndPlugin.Core.Contracts;

namespace UI.Web.MVC.Moduls.News.Controllers
{
    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Web.Mvc;
    using MvcContrib.Sorting;
    using MvcContrib.UI.Grid;
    using SailHero.Soft.FrameWork.IDomainRepositories.Contracts;
    using SailHero.Soft.FrameWork.Models;
    using UI.Web.Utility.MvcPager;
    using UI.Web.MVC.Common;
    /// <summary>
    /// Manages actions of the blog.  
    /// </summary>
    [Export(typeof(IController)), ExportMetadata("Name", "News")]
    public class NewsController : Controller
    {

        #region Fileds

        [Import(typeof(IApplication))]
        public IApplication ApplicationContainer { get; set; }

        #endregion

        #region Actions
        /// <summary>
        /// Displays the blog index.
        /// </summary>
        [Authorize]
        public ActionResult Index()
        {
            //return RedirectToAction("Index", "Home");

            var md = ApplicationContainer.FindCmdByKey("GetVollContent");
            if(md!=null)
            {
                ViewBag.Content = md.DynamicInvoke(null);
            }
            if (ViewBag.Content == null)
            {
                ViewBag.Content = "无";
            }
            return View();
        }

        /// <summary>
        /// 数据操作接口
        /// </summary>
        [Import]
        public ITB_CLIENTSRepository tB_CLIENTSRepository;
        /// <summary>
        /// Displays a posted blog article.
        /// </summary>
        /// <param name="id">The Id of the article to display.</param>
        [Authorize]
        public ActionResult View([DefaultValue(1)] int pageIndex, [DefaultValue(null)]GridSortOptions sort)
        {
            ViewData["sort"] = sort;
            if (sort.Column == null)
            {
                sort.Column = "ID";
            }
            var lstData = tB_CLIENTSRepository.GetList(itm => itm.ID > 0).ToList();
            int pageSize = 10;
            PagedList<TB_CLIENTS> pagedOrders = new PagedList<TB_CLIENTS>(lstData, pageIndex, pageSize, lstData.Count);
            if (!string.IsNullOrEmpty(sort.Column))
            {
                var orders0 = lstData.OrderBy(sort.Column, sort.Direction).Skip(((pageIndex - 1) * pageSize)).Take(pageSize);
                ViewBag.ListData = orders0;
            }
            else
            {
                ViewBag.ListData = lstData.Skip(((pageIndex - 1) * pageSize)).Take(pageSize);
            }
            //if (Request.IsAjaxRequest())
            //    return PartialView("UcNewsGrid", pagedOrders);
            return View(pagedOrders);
            //return View(new Post() { Title = "Test Blog Post" });
        }

        /// <summary>
        /// Displays a posted blog article.
        /// </summary>
        /// <param name="id">The Id of the article to display.</param>
        [Authorize]
        public ActionResult LeftJoin([DefaultValue(1)] int pageIndex, [DefaultValue(null)]GridSortOptions sort)
        {
            ViewData["sort"] = sort;
            if (sort.Column == null)
            {
                sort.Column = "UserID";
            }



            var source = tB_CLIENTSRepository.GetLeftJoinData().ToList();
            if (source == null || source.Count <= 0)
            {
                return null;
            }
            List<Sys_UserRole> lstUsR = source.ToList();
            var lstData = lstUsR;
            int pageSize = 10;
            PagedList<Sys_UserRole> pagedOrders = new PagedList<Sys_UserRole>(lstData, pageIndex, pageSize, lstData.Count);
            if (!string.IsNullOrEmpty(sort.Column))
            {
                var orders0 = lstData.OrderBy(sort.Column, sort.Direction).Skip(((pageIndex - 1) * pageSize)).Take(pageSize);
                ViewBag.ListData = orders0;
            }
            else
            {
                ViewBag.ListData = lstData.Skip(((pageIndex - 1) * pageSize)).Take(pageSize);
            }
            //if (Request.IsAjaxRequest())
            //    return PartialView("UcNewsGrid", pagedOrders);
            return View(pagedOrders);
            //return View(new Post() { Title = "Test Blog Post" });
        }

        #endregion
    }
}